// proxyIncomeDetail.js
const app = getApp()
const util = require("../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: '0',
    tablineStyle: '',

    amount: 0,
    deviceCount: 0,
    orderCount: 0,
    order:[],

    pagenum: 1,

    
  },
  // 切换tab
  tabChoose: function (e) {
    var that = this;
    var cur = e.target.dataset.current;
    var type = cur == '0' ? 'self' : 'other';
    if (that.data.currentTab == cur) { return false; }
    else {
      that.setData({
        currentTab: cur,
        order:[],
        pagenum: 1
      })
    }
    that.tabLine();
    that.incomeStatistics(type);

  },
  // 切换tab滑块动画
  tabLine: function () {
    var query = wx.createSelectorQuery();
    var that = this;
    var currentTab = that.data.currentTab;
    query.select('.tab').boundingClientRect(function (rect) {
      that.setData({
        tablineStyle: "width:" + rect.width / 2 + "px;transform: translateX(" + rect.width * currentTab / 2 + "px);"
      })
    }).exec();
  },
  incomeStatistics: function (type, isScroll){
    var that = this;
    if (isScroll !== 'scroll') {
      console.log('isScrollfalse')
      that.setData({
        order: [],
      })
    }
    var data = { page:that.data.pagenum,type:type};
    app.post('agent/incomeStatistics', data).then((res) => {
      if (res.code == 200) {
        var list = that.data.order;
        var dataArr = res.data.order;
        list = list.concat(dataArr)
        that.setData({
          amount: res.data.amount,
          deviceCount: res.data.deviceCount,
          orderCount: res.data.orderCount,
          order: list
        })

      } 
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var type = this.data.currentTab == '0' ? 'self' : 'other';
    this.tabLine();
    this.incomeStatistics(type);  
    wx.stopPullDownRefresh()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      list: [],
      pagenum: 1
    })
    this.onLoad();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var type = this.data.currentTab == '0' ? 'self' : 'other';
    var pagenum = that.data.pagenum + 1; //获取当前页数并+1
    that.setData({
      pagenum: pagenum, //更新当前页数
    })
    that.incomeStatistics(type,'scroll');//重新调用请求获取下一页数据
  }
})